package com.zeekr.orm.exception;

import com.zeekr.orm.common.BaseResponse;
import com.zeekr.orm.common.ErrorCode;
import com.zeekr.orm.common.ResultUtils;
import com.zeekr.orm.utils.ThreadPoolInfo;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.concurrent.RejectedExecutionException;

/**
 * 全局异常处理器
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://yupi.icu">编程导航知识星球</a>
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

  @ExceptionHandler(RejectedExecutionException.class)
  public BaseResponse<?> threadExceptionHandler(RejectedExecutionException e) {
    int threadPoolSize = new ThreadPoolInfo().getThreadPoolSize();
    System.out.println(threadPoolSize);
    return ResultUtils.error(ErrorCode.SYSTEM_Task_OUT, ErrorCode.SYSTEM_Task_OUT.getMessage());
  }

  @ExceptionHandler(BusinessException.class)
  public BaseResponse<?> businessExceptionHandler(BusinessException e) {
    return ResultUtils.error(e.getCode(), e.getMessage());
  }

  @ExceptionHandler(RuntimeException.class)
  public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
    return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
  }

}
